import os
import pandas as pd
from datetime import datetime
import urllib.parse
import requests
from config.settings import CATEGORIES


def save_data(data, source_name, date_str):
    """保存数据到CSV"""
    if not data:
        return

    # 创建每日文件夹
    folder_path = os.path.join('data', date_str)
    os.makedirs(folder_path, exist_ok=True)

    # 保存CSV
    df = pd.DataFrame(data)
    file_path = os.path.join(folder_path, f"{source_name}_{date_str}.csv")
    df.to_csv(file_path, index=False, encoding='utf-8-sig')

    return file_path


def save_data_by_category(data, source_name, date_str):
    """
    按分类保存数据到CSV
    
    Args:
        data (list): 包含分类信息的文章数据
        source_name (str): 数据源名称
        date_str (str): 日期字符串
    """
    if not data:
        return

    # 按分类保存数据
    for category in CATEGORIES:
        # 筛选属于该分类的文章
        category_articles = [article for article in data if category in article.get('分类', ['其他'])]
        
        if category_articles:
            # 创建分类文件夹
            category_path = os.path.join('data', 'categories', category)
            os.makedirs(category_path, exist_ok=True)
            
            # 保存CSV
            df = pd.DataFrame(category_articles)
            file_path = os.path.join(category_path, f"{source_name}_{date_str}.csv")
            df.to_csv(file_path, index=False, encoding='utf-8-sig')


def download_attachments(attachments, folder_path):
    """下载附件"""
    downloaded = []

    for attach in attachments:
        try:
            response = requests.get(attach['url'], stream=True)
            response.raise_for_status()

            # 获取文件名
            filename = urllib.parse.unquote(os.path.basename(attach['url']))
            if not filename:
                filename = attach['name'] + '.pdf'

            file_path = os.path.join(folder_path, filename)

            with open(file_path, 'wb') as f:
                for chunk in response.iter_content(8192):
                    f.write(chunk)

            downloaded.append(file_path)

        except Exception as e:
            print(f"下载附件失败: {attach['url']}, 错误: {str(e)}")

    return downloaded